import "package:flutter/material.dart";

// ignore: must_be_immutable
class HotTrending extends StatelessWidget {
  List<String> hotTrendingList = [
    '特朗普获共和党正式题名',
    '拜登评价万斯是特朗普的克隆人',
    '劳斯莱斯前设计师遇刺身亡',
    '首位票房破200亿女主演',
    '中国移动回应收来电显示费',
    '谁发明的这种旅行转场',
    '默杀暂居暑期档票房首位',
    '王者梦之队大名单',
  ];
  HotTrending({super.key});

  @override
  Widget build(BuildContext context) {
    return SliverToBoxAdapter(
      child: Container(
        width: double.infinity,
        padding: const EdgeInsets.only(left: 12),
        decoration: const BoxDecoration(
          color: Colors.white,
        ),
        child: Container(
          decoration: const BoxDecoration(
            border: Border(
              top: BorderSide(
                width: 0.3,
                color: Color.fromARGB(255, 207, 207, 207),
              ),
            ),
          ),
          child: Column(
            children: [
              const Padding(
                padding: EdgeInsets.only(top: 8, bottom: 8, right: 6),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Row(
                      children: [
                        Icon(
                          Icons.tag,
                          size: 16,
                        ),
                        Text(
                          '热门话题',
                          style: TextStyle(
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                      ],
                    ),
                    Row(
                      children: [
                        Text(
                          '查看更多',
                          style: TextStyle(
                            color: Colors.black38,
                            fontSize: 12,
                          ),
                        ),
                        Icon(
                          Icons.keyboard_arrow_right_outlined,
                          color: Colors.black38,
                          size: 18,
                        ),
                      ],
                    )
                  ],
                ),
              ),
              HotTrendingList(hotTrendingList: hotTrendingList),
            ],
          ),
        ),
      ),
    );
  }
}

// ignore: must_be_immutable
class HotTrendingList extends StatelessWidget {
  List<String> hotTrendingList;
  HotTrendingList({super.key, required this.hotTrendingList});

  @override
  Widget build(BuildContext context) {
    // 预处理
    List<List<String>> list = [];
    if (hotTrendingList.length % 2 != 0) hotTrendingList.add('');
    // 按每行两个分割
    List<String> row = [];
    for (int i = 0; i < hotTrendingList.length; i++) {
      if (row.length == 2) {
        list.add(row);
        row = [];
      }
      if (row.length != 2) {
        row.add(hotTrendingList[i]);
      }
      if (i == hotTrendingList.length - 1) list.add(row);
    }

    List<Widget> rowWidgetList = [];
    for (List<String> element in list) {
      List<Widget> children = [];
      for (String title in element) {
        children.add(
          Expanded(
            child: Container(
              margin:const EdgeInsets.fromLTRB(0, 0, 8, 10),
              child: Text(
                title,
                maxLines: 1,
                style: const TextStyle(
                  overflow: TextOverflow.ellipsis,
                  color: Color.fromARGB(255, 33, 33, 33),
                ),
              ),
            ),
          ),
        );
      }
      rowWidgetList.add(Row(
        children: children,
      ));
    }
    return Container(
      padding: const EdgeInsets.only(right: 10),
      child: Column(
        children: rowWidgetList,
      ),
    );
  }
}
